<template>
    <PopUps :bgIndex="1000"  :boxIndex="1001" widthMy='60%'  height-my="78%">
        <div class="popups-box">
            <div class="header">
                <div class="header-top">
                    <h3>修改施工队信息</h3>
                    <i class="iconfont icon-icon-cross-empty" @click="closePopUpsUpdate"></i>
                </div>
                <div class="headerMain">
                    <el-form ref="formRef" :inline="true" :model="popUpsForm" :rules="rules" class="demo-form-inline">
                        <el-form-item :prop="item.name" :label="item.label" v-for="(item, index) in popUpsFormLabel"
                            :key="index" :style="{ 'min-width': `${item.minWidth}'!important'` }">
                            <!-- 输入框 -->
                            <div class="optionsInput">
                                <el-input v-model="popUpsForm[item.name]" clearable
                                    v-if="!item.type || item.type == 'input'" />
                            </div>
                            <!-- 下拉选框 -->
                            <el-select v-model="popUpsForm[item.name]" clearable v-if="item.type == 'select'"
                                @change="headerMainselectChange(item.name, $event)">
                                <el-option :label="item1.label" :value="item.paramsName ? item1.label : item1.value"
                                    v-for="(item1, index) in item.client" :key="index" />
                            </el-select>
                            <!-- 日期选框 -->
                            <el-date-picker v-else-if="item.type == 'picker'" v-model="popUpsForm[item.name]" type="date"
                                placeholder="请输入日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="main">
                <div class="main-top">
                    <h3>施工队付款信息</h3>
                </div>
                <div class="mainContent">
                    <ForFormCom :Form="basicInformationForm" />
                    <el-form :inline="true" :model="basicInformationForm" class="demo-form-inline"
                        :class="props.className || ''">
                        <el-form-item label="施工队月结">
                            <!-- 输入框 -->
                            <el-input v-model="basicInformationForm.constructionTeamMonthEnd" clearable />
                        </el-form-item>
                        <el-form-item label="施工队临时支付">
                            <!-- 输入框 -->
                            <el-input v-model="basicInformationForm.constructionTeamTemporaryPayment" clearable />
                        </el-form-item>

                    </el-form>
                    <div class="label-flex remarks-box" style="justify-content: space-between ;">
                        <div style="display: flex; align-items: center;">
                            <p class="lable-p remarks">备注</p>
                            <el-input v-model="popUpsForm.remarks" type="textarea" />
                        </div>
                        <div class="btn">
                            <el-button type="primary" @click="onSubmit(formRef)" class="submit">
                                保存</el-button>
                            <el-button type="primary" @click="closePopUpsUpdate" class="reset"
                                color="#E4E9FC">取消</el-button>
                        </div>
                    </div>
                    <div class="main-title">
                        <h3>施工队人员基本信息</h3>
                    </div>
                    <div class="table">
                        <customerTable :tableHeaderData="tableHeaderData" :tableData="tableData" :operate="true"
                            :bg="'#F6F8FE'" @handleDelete="handleDelete" @handleSave="handleSave" :disabled="disabled"
                            :customerTableSave="customerTableSave" />
                        <div class="addTable">
                            <span @click="addTable">增加一行</span>
                        </div>
                    </div>

                </div>
            </div>
            .
        </div>
    </PopUps>
</template>
<script setup>
import PopUps from '@/components/My/PopUps';
// import addTable from './components/addTable';
import { reactive, ref, defineEmits, watch, onMounted, defineProps } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getdepartmentseApi } from '@/api/tickets/index'
import customerTable from '../../customer/components/customerTable';
import ForFormCom from '@/components/ForFormCom/index'
import { listPeople, updateTeams, updatePeople, addPeople, delPeople } from '@/api/teams'
import { getEmployeeApi } from '@/api/tickets/index'
const { proxy } = getCurrentInstance();



let customerTableSave = ref(true)
const emit = defineEmits(['popUpsFlase', 'getoutboundOrderData'])
let props = defineProps(['PopData', 'constructionTeamList'])
let flag = ref(false)
// 部门下拉数据
let deptId = ref([])
const formRef = ref(null)

// 封装请求下拉菜单信息
const getDropDownData = (res) => {
    let { code, data } = res
    if (code == 200) {
        return data
    }
}
// 表单
let popUpsForm = ref({
    constructionTeamName: '',               // 施工队名称
    constructionPhone: '',              // 施工队电话
    responsiblePerson: '',               // 负责人
    constructionTeamType: '',            // 施工队类别
    constructionTeamInsuranceType: '',             // 施工队保险类别
    responsiblePersonPhone: '',                 // 负责人电话
    constructionTeamRank: '',               // 施工队等级
    constructionTeamRegion: '',          // 施工队区域
    agent: '',                    // 经办人
    remarks: '',                    // 备注
})
// 表单数据
let popUpsFormLabel = ref([
    {
        name: 'constructionTeamName',
        label: '施工队名称',
    },
    {
        name: 'constructionPhone',
        label: '施工队电话',
    },
    {
        name: 'responsiblePerson',
        label: '负责人',
        type: 'select',
        client: [
            {
                label: '入库仓库1',
                value: '0'
            },
            {
                label: '入库仓库2',
                value: '1'
            },
            {
                label: '入库仓库3',
                value: '2'
            }
        ]
    },
    {
        name: 'constructionTeamType',
        label: '施工队类别',
        type: 'select',
        client: [
            {
                label: '固定施工队',
                value: '0'
            },
            {
                label: '临时施工队',
                value: '1'
            },
        ]
    },

    {
        name: 'constructionTeamInsuranceType',
        label: '施工队保险',
    },
    {
        name: 'responsiblePersonPhone',
        label: '负责人电话',
    },
    {
        name: 'constructionTeamRank',
        label: '施工队等级',
        type: 'select',
        client: [
            {
                label: 'A级',
                value: 'A级'
            },
            {
                label: 'B级',
                value: 'B级'
            },
            {
                label: 'C级',
                value: 'C级'
            },
            {
                label: 'D级',
                value: 'D级'
            },
        ]
    },
    {
        name: 'constructionTeamRegion',
        label: '施工队区域',
    },
    {
        name: 'agent',
        label: '经办人',
    },
])
// 表格内容
const tableData = ref([
    {
        name: '',       // 姓名
        gender: '',     // 性别
        contactPhone: '',   // 联系电话
        address: '',        // 地址
    }
])

// 表格头部
const tableHeaderData = ref([
    {
        prop: 'name',
        label: '姓名',
        width: '150px'
    },
    {
        prop: 'gender',
        label: '性别',
        width: '150px'
    },
    {
        prop: 'contactPhone',
        label: '联系电话',
        width: '150px'
    },
    {
        prop: 'address',
        label: '地址',
        colorClassName: 'addressInput',
        width: '300px'
    },
])
// 出库明细表单
let basicInformationForm = ref(
    {
        constructionTeamMonthEnd: '',      // 施工队月结
        constructionTeamTemporaryPayment: '',    // 施工队临时支付
    }
)

// 出库信息校验
const rules = reactive({
    constructionTeamName: [
        {
            required: true,
            message: '施工队名称',
            trigger: 'change',
        }
    ],
    responsiblePerson: [
        {
            required: true,
            message: '负责人',
            trigger: 'change',
        },
    ],
    constructionTeamType: [
        {
            required: true,
            message: '施工队类别',
            trigger: 'change',
        }

    ],
    constructionTeamInsuranceType: [
        {
            required: true,
            message: '施工队保险类别',
            trigger: 'change',
        },
    ],
    responsiblePersonPhone: [
        {
            required: true,
            message: '负责人电话',
            trigger: 'change',
        },
        {
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur'
        }
    ],
    constructionTeamRank: [
        {
            required: true,
            message: '施工队等级',
            trigger: 'change',
        },
    ],
    constructionTeamRegion: [
        {
            required: true,
            message: '施工队区域',
            trigger: 'change',
        }
    ],
    agent: [
        {
            required: true,
            message: '经办人',
            trigger: 'change',
        },
    ],
})


// 修改接口
const onSubmit = (formRef) => {
    ElMessageBox.confirm(
        '确认保存修改吗?',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'success',
        }
    ).then(async () => {
        if (!formRef) return
        await formRef.validate((valid) => {
            if (valid) {
                // 发送新增请求
                console.log('发送新增请求', basicInformationForm.value);
                let params = Object.assign(popUpsForm.value, basicInformationForm.value)
                params.constructionTeamId = props.constructionTeamList.constructionTeamId
                updateTeams(params).then(res => {
                    if (res.code == 200) {
                        emit('getoutboundOrderData')
                        closePopUpsUpdate()
                        ElMessage({
                            type: 'success',
                            message: res.msg,
                        })
                    }
                })
            }
        })
    })
}
// 一级弹窗选择下拉事件
const headerMainselectChange = (name, el) => {
    switch (name) {
        case 'agent':
            // getdeptIdseApiData(el, false)
            break;

        default:
            break;
    }
}
// 关闭
const closePopUpsUpdate = () => {
    emit('popUpsFlaseUpdate', false)
}
// 获取部门下拉菜单数据
const getdeptIdseApiData = async (data = '') => {
    let params = { agent: data }
    let res = await getdepartmentseApi(params)
    deptId.value = getDropDownData(res)
}

// 新增一行表格
const addTable = () => {
    tableData.value.push({
        name: '',
        gender: '',
        office: '',
        supplieDept: '',
        contactPhone: '',
        address: '',
    })
}
// 删除一行表格
const handleDelete = async (v) => {
    if (tableData.value[v.i].constructionPeopleId) {
        let { msg, code } = await delPeople(tableData.value[v.i].constructionPeopleId)
        if (code == 200) {
            listPeopleFn()
            proxy.$modal.msgSuccess(msg);

        }
    } else {
        tableData.value.splice(props.i, 1)
    }
}
// 保存
const handleSave = async (v) => {
    console.log(v, 'v');
    if (tableData.value[v.i].constructionPeopleId) {
        let { msg, code } = await updatePeople(tableData.value[v.i])
        if (code == 200) {
            proxy.$modal.msgSuccess(msg);
        }
    } else {
        tableData.value[v.i].constructionTeamId = props.constructionTeamList.constructionTeamId
        let { msg, code } = await addPeople(tableData.value[v.i])
        if (code == 200) {
            proxy.$modal.msgSuccess(msg);
        }
    }

}

// 获取业务员下拉菜单数据
const getEmployeeApiData = async (data = '') => {
    let params = { employeeName: data }
    let res = await getEmployeeApi(params)
    console.log(res);
    if (res.code == 200) {

        popUpsFormLabel.value[3].client = res.data.map((v) => {
            return {
                label: v.employeeName,
                value: v.employeeId
            }
        })
    }
}

const callbackData = () => {
    console.log(props.constructionTeamList);
    for (const key in popUpsForm.value) {
        popUpsForm.value[key] = props.constructionTeamList[key]
    }
    for (const key in basicInformationForm.value) {
        basicInformationForm.value[key] = props.constructionTeamList[key]
    }
}
const listPeopleFn = async () => {
    let params = {
        constructionTeamId: props.constructionTeamList.constructionTeamId
    }
    let { rows, code } = await listPeople(params)
    if (code == 200) {
        tableData.value = rows
    }
}

watch(() => tableData.value, (val) => {
    if (val.length == 0) {
        addTable()
    }
})
onMounted(() => {
    getdeptIdseApiData()
    getEmployeeApiData()
    if (props.constructionTeamList) {
        callbackData()
        listPeopleFn()
    }
})
</script>

<style lang="scss" scoped>
.demo-form-inline .el-select {
    --el-select-width: 220px;
}

@mixin flex {
    display: flex;
    align-items: center;
    margin-bottom: 18px;
}

@mixin label {
    width: 120px;
    text-align: right;
    padding-right: 12px;
    font-size: 14px;
}



.form-inline {
    :deep(.el-form-item__label) {
        width: 80px;
    }

    :deep(.el-form-item) {
        width: 190px;
    }
}

.el-input__wrapper {
    --el-select-width: 220px;
}

:deep(.el-form-item) {
    width: 235px !important;
}

:deep(.el-form-item__label) {
    width: 124px;
    text-align: right;
}

.optionsInput {
    display: flex;
    align-items: center;

    .btn {
        background-color: #829BFF;
        color: #fff;
        margin-left: 15px;
        width: 50px;
        height: 25px;

    }
}

@mixin publicH3 {
    font-weight: 550;
    position: relative;
    font-size: 16px !important;

    &::after {
        content: '';
        width: 5px;
        height: 20px;
        background-color: #1947FF;
        border-radius: 5px;
        position: absolute;
        left: -15px;
        top: -2px;
    }
}

@mixin titlePadding {
    padding: 25px 25px 25px 45px;
}

@mixin pubilcForm {

    :deep(.el-form-item__label) {
        font-size: 14px;
    }

    :deep(.el-input__wrapper) {
        width: 132px;
    }

    :deep(.el-date-editor--datetimerange) {
        width: 240px;
    }

    :deep(.el-table .el-table__header-wrapper th, .el-table .el-table__fixed-header-wrapper th) {
        font-size: 15px !important;
    }
}

.popups-box {

    .header {
        .header-top {
            @include titlePadding;
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: sticky;
            top: 0px;
            background: #FFF;
            z-index: 2024;

            h3 {
                @include publicH3;
            }

            .iconfont {
                color: #666;
                font-size: 27px;
            }
        }

        .headerMain {
            .demo-form-inline {
                @include pubilcForm;
            }
        }
    }

    .main {
        .main-top {
            @include titlePadding;

            h3 {
                @include publicH3;
            }
        }

        .mainContent {
            padding: 0 20px;

            .label-flex {
                @include flex
            }

            .main-title {
                padding: 25px 25px 25px 25px;

                h3 {
                    @include publicH3;
                }
            }

            :deep(.el-form) {
                display: flex;
                align-items: center;

                .el-form-item {
                    display: flex;
                    align-items: center;
                }

                .el-form-item__content,
                .el-input__inner {
                    height: 30px;
                }
            }

            .table {
                padding: 0 90px;

                .addTable {
                    width: 100%;
                    text-align: right;
                    margin: 20px 0;
                    color: #1947FF;
                    cursor: pointer;
                }
            }

            .lable-p {
                @include label
            }

            .remarks-box {
                .remarks {
                    width: 120px;
                }

                :deep(.el-textarea) {
                    width: 180px;
                }
            }

            .remarks {
                width: 400px;
            }

            .demo-form-inline {
                @include pubilcForm;
            }

            .btn {
                text-align: right;

                .el-button {
                    width: 50px;
                    height: 25px;
                    font-size: 12px;
                }

                .reset {
                    color: #333;
                    border: none;
                }
            }
        }
    }
}

.optionsOrder {
    padding: 20px;
    height: 100%;

    header {
        margin-bottom: 20px;
        font-weight: 550;
    }



    .btn {
        width: 100%;
        text-align: right;
        margin-top: 10px;

        .el-button {
            width: 50px;
            height: 25px;
            font-size: 12px;
        }
    }
}
</style>